package employee.web.beans;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import org.primefaces.event.RowEditEvent;

import dtos.MeanOfTransportationDTO;
import dtos.TransportationTypeDTO;
import dtos.TripDTO;
import employee.ejb.management.ProductMgr;

@SuppressWarnings("serial")
@ManagedBean(name="transportationBean")
@ViewScoped
public class TransportationBean implements Serializable{

	@EJB
	private ProductMgr proMgr;
	
	private TripDTO selectedTrip;
	private TransportationTypeDTO selectedType;
	private MeanOfTransportationDTO selectedMean;
	
	private DataModel<TripDTO> _tripModel;
	private DataModel<TransportationTypeDTO> _typeModel;
	private DataModel<MeanOfTransportationDTO> _meanModel;
		
	

    @PostConstruct
	public void refresh(){
    	List<TripDTO> trips = new LinkedList<>();
    	List<TransportationTypeDTO> transtypes = new LinkedList<>();
    	List<MeanOfTransportationDTO> means= new LinkedList<>();
    	
    	for(TripDTO trip : proMgr.getTripList()){

    		if (!(trip.getDeparturePlace().contains("(not available)"))){
        		trips.add(trip);
    		}
    	}
			
    	for(TransportationTypeDTO transtype : proMgr.getTypeList())
			transtypes.add(transtype);
    	for(MeanOfTransportationDTO mean : proMgr.getMeanList())
    		means.add(mean);
    	
    	_tripModel = new ListDataModel<>(trips);
    	_typeModel = new ListDataModel<>(transtypes);
    	_meanModel = new ListDataModel<>(means);
    	
	}
    
    public TripDTO getSelectedTrip() {  
        return selectedTrip;  
    }  
  
    public void setSelectedTrip(TripDTO selectedTrip) {  
        this.selectedTrip = selectedTrip;  
    }
    
    public TransportationTypeDTO getSelectedType() {  
        return selectedType;  
    }  
  
    public void setSelectedType(TransportationTypeDTO selectedType) {  
        this.selectedType = selectedType;  
    }
    
    public MeanOfTransportationDTO getSelectedMean() {  
        return selectedMean;  
    }  
  
    public void setSelectedMean(MeanOfTransportationDTO selectedMean) {  
        this.selectedMean = selectedMean;  
    }
	
    public DataModel<TripDTO> getTripModel(){
    	return _tripModel;
    }
    
    public DataModel<TransportationTypeDTO> getTypeModel(){
    	return _typeModel;
    }
    
    public DataModel<MeanOfTransportationDTO> getMeanModel(){
    	return _meanModel;
    }
    
    public void onEditType(RowEditEvent event) { 
		TransportationTypeDTO tdto = new TransportationTypeDTO();
		tdto.setTransportationTypeID(((TransportationTypeDTO) event.getObject()).getTransportationTypeID());
		tdto.setType(((TransportationTypeDTO) event.getObject()).getType());
		tdto.setCompany(((TransportationTypeDTO) event.getObject()).getCompany());
		tdto.setFlightClass(((TransportationTypeDTO) event.getObject()).getFlightClass());
		tdto.setTrainClass(((TransportationTypeDTO) event.getObject()).getTrainClass());
		tdto.setVehicleType(((TransportationTypeDTO) event.getObject()).getVehicleType());
		
		proMgr.updateType(tdto);
		
        FacesMessage msg = new FacesMessage("Transportation Type Edited");  
  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }
    
  
}
